package com.taotao.content.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.scenario.effect.impl.prism.PrImage;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.IContentService;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author zhangxiaolin@zhonhajt.com
 * @Title: 内容业务实现类
 * @Package com.taotao.content.service.impl
 * @Description:
 * @date 2018/10/19 16:04
 */
@Service
public class ContentServiceImpl implements IContentService {

    @Autowired
    private TbContentMapper contentMapper;

    @Override
    public EasyUIDataGridResult getContentList(long categoryId, int page, int rows) {
        PageHelper.startPage(page, rows);

        TbContentExample example = new TbContentExample();
        example.createCriteria().andCategoryIdEqualTo(categoryId);

        List<TbContent> contentList = contentMapper.selectByExample(example);

        PageInfo<TbContent> pageInfo = new PageInfo<>(contentList);
        return new EasyUIDataGridResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public TaotaoResult saveContent(TbContent tbContent) {
        tbContent.setCreated(new Date());
        tbContent.setUpdated(new Date());

        int count = contentMapper.insert(tbContent);
        if (count > 0) {
            return TaotaoResult.ok();
        }

        return TaotaoResult.error("插入失败");
    }
}
